package com.witframework.web.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
 * web message
 * @author wuxinyang
 *
 */
public class MessageFilter implements Filter{
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest) req;
        // grab messages from the session and put them into request
        // this is so they're not lost in a redirect
        List messages = (List) request.getSession().getAttribute("messages");       
        if (messages != null){
            request.setAttribute("messages", messages);
            request.getSession().removeAttribute("messages");
        }   
        List successMessages = (List) request.getSession().getAttribute("actionMessages");       
        if (successMessages != null){
            request.setAttribute("actionMessages", successMessages);
            request.getSession().removeAttribute("actionMessages");
        }       
        List fieldErrors = (List) request.getSession().getAttribute("fieldErrors");       
        if (fieldErrors != null){
            request.setAttribute("fieldErrors", fieldErrors);
            request.getSession().removeAttribute("fieldErrors");
        }
        
//        String messagerequestids=(String)request.getSession().getAttribute("messagerequestids");
//        if (messagerequestids != null){
//            request.setAttribute("messagerequestids", messagerequestids);
//            request.getSession().removeAttribute("messagerequestids");
//        }       
        
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig){

    }

    public void destroy(){
    }
    
    

}

